/******************************************************************************/
package jamie.org.sil.www.usxUtils;

import java.util.Collection;
import java.util.Iterator;
import java.util.TreeMap;



/******************************************************************************/
/**
   Base class to enable numerals to be converted into the appropriate
   vernacular form.
   <p>

   I'm not 100% sure this is really needed -- purportedly Java and Unicode
   between them should be capable of doing most number translations.
   However, we haven't proved that for sure, so it's probably safest to retain
   this for the time being at least.
*/

public abstract class NumeralProcessor
{
  /****************************************************************************/
  /****************************************************************************/
  /**                                                                        **/
  /**                               Public                                   **/
  /**                                                                        **/
  /****************************************************************************/
  /****************************************************************************/

  /****************************************************************************/
  /** Singleton.
   *
   * @return Instance.
   */

  static public NumeralProcessor instance ()
  {
    return m_Instance;
  }


  /****************************************************************************/
  /**
     Initialise the new instance.  You shouldn't normally need to
     understand much about the parameters, because I don't really
     intend this class to be created directly &endash; there's a
     factory class (NumeralProcessorFactory) which creates instances
     and performs all the necessary initialisation.

     @param integerToGlyph Decimal number to vernacular character mapping.

     @param textDir LTR / RTL etc.

     @param base Number base.

     @return Singleton instance.
  */

  public void initialise (TreeMap<Integer, String> integerToGlyph,
                          String textDir,
                          int base)
  {
    m_Base = base;
    m_IntegerToGlyph = integerToGlyph;
    m_TextDir = textDir;

    Collection c = integerToGlyph.keySet();
    Iterator it = c.iterator();
    while(it.hasNext())
    {
      int i = (Integer) it.next();
      m_GlyphToInteger.put(integerToGlyph.get(i), i);
    }

    setInstance();
  }


  /****************************************************************************/
  /**
     Convert a number from vernacular form to an integer.

     @param n Number to be converted, as a string in vernacular form (positive
              integers only).

     @return Integer representation.
  */

  abstract public int fromVernacular (String n);


  /****************************************************************************/
  /**
     Convert a given number to vernacular form.

     @param n Number to be converted, as a string in standard European
              decimal format (positive integers only).

     @return Vernacular form.
  */

  public String toVernacular (String n)
  {
    return toVernacular(Integer.parseInt(n));
  }


  /****************************************************************************/
  /**
     Convert a given number to vernacular form.

     @param n Number to be converted.

     @return Vernacular form.
  */

  abstract public String toVernacular (int n);


  /****************************************************************************/
  /** Permits the subclass to set the singleton instance member.
   */

  abstract public void setInstance();





  /****************************************************************************/
  /****************************************************************************/
  /**                                                                        **/
  /**                        Protected variables                             **/
  /**                                                                        **/
  /****************************************************************************/
  /****************************************************************************/

  /**************************************************************************/
  /** Number base */
  protected int m_Base = 0;

  /** Singleton instance. */
  protected static NumeralProcessor m_Instance = null;

  /** Maps single digits to the corresponding glyph. */
  protected TreeMap<Integer, String> m_IntegerToGlyph = null;

  /** Maps glyphs to the corresponding digit. */
  protected TreeMap<String, Integer> m_GlyphToInteger = new TreeMap<String, Integer>();

  /** LTR (left-to-right) or RTL (right-to-left). */
  protected String m_TextDir = "";
}
